<%--
  Created by IntelliJ IDEA.
  User: Cc
  Date: 2020/12/30
  Time: 16:51
  To change this template use File | Settings | File Templates.
--%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" +
            request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="shortcut icon" href="/static/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="/static/css/font.css">
    <link rel="stylesheet" href="/static/css/xadmin.css">
    <link rel="stylesheet" href="/static/css/bootstrap.css">
    <script type="text/javascript" src="/static/js/jquery-2.1.1.min.js"></script>
    <script type="text/javascript" src="/static/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="/static/js/jqPaginator.js"></script>
    <script type="text/javascript" src="/static/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/static/js/xadmin.js"></script>

</head>

<body>
<div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="#">型号列表</a>
      </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
</div>
<div class="x-body">
    <div class="layui-row">
        <form class="layui-form layui-col-md12 x-so">
            <input type="text" placeholder="型号/名字" id="condition" autocomplete="off" class="layui-input">
            <button class="layui-btn" type="button" onclick="findModel()"><i class="layui-icon">&#xe615;</i></button>
        </form>
    </div>

    <xblock>
        <button class="layui-btn" onclick="x_admin_show('添加型号信息','/jsp/model-add.jsp',500,450)"><i class="layui-icon"></i>添加
        </button>
        <span class="x-right" style="line-height:40px" id="showTotal">共有数据： 条</span>
    </xblock>
    <table class="layui-table">
        <thead>
        <tr>
            <th>型号</th>
            <th>名字</th>
            <th>备注</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody id="tab"></tbody>
        <tr>
            <td colspan="8" align="center">
                <ul class="pagination" id="pagination1"></ul>
            </td>
        </tr>
    </table>
</div>
<script>
    $(document).ready(function enterJudge(e){
        $(this).keydown(function (e) {
            if (e.which == "13" && $("#condition").val()!=null) {
                findModel();
                return false;
            }
        })
    })
    //总页数
    let totalPages=0;
    //总条数
    let totalCounts=0;
    //当前页
    let currentPage=0;
    $(function () {
        findGoodsModelAll(1);
        pages();
    })
    function pages() {
        $("#pagination1").jqPaginator({
            //总页数
            totalPages:totalPages,
            //总条数
            totalCounts:totalCounts,
            //当前页
            currentPage:currentPage,
            //加载按钮
            first:'<li class="first"><a href="javascript:void(0)">首页</a></li>',
            prev:'<li class="prev"><a href="javascript:void(0)">上一页</a></li>',
            next:'<li class="next"><a href="javascript:void(0)">下一页</a></li>',
            last:'<li class="last"><a href="javascript:void(0)">末页</a></li>',
            page:'<li class="page"><a href="javascript:void(0)">{{page}}</a></li>',
            onPageChange: function (number) {
                findGoodsModelAll(number);
            }
        })
    }
    function findGoodsModelAll(number) {
        $.ajax({
            url:"../gm/findGoodsModelAll",
            type:"POST",
            async:false,
            data:{
                "pageNum":number,
                "pageSize":3,
                "condition":$("#condition").val(),
            },
            dataType:"json",
            success:function (data) {
                console.log(data);
                let models = data.modelInfo.list;
                if (models.length>0){
                    totalPages=data.modelInfo.pages;
                    totalCounts=data.modelInfo.total;
                    currentPage=data.modelInfo.pageNum;
                    let str="";
                    for (let i=0;i<models.length;i++) {
                        if(models[i].mname==null){
                            models[i].mname="";
                        }
                        if(models[i].mremarks==null){
                            models[i].mremarks="";
                        }
                        str+='<tr>'+
                            '<td>'+models[i].mtype+'</td>'+
                            '<td>'+models[i].mname+'</td>'+
                            '<td>'+models[i].mremarks+'</td>'+
                            '<td class="td-manage"><div class="layui-btn-group"><button type="button" class="layui-btn layui-btn-sm" onclick="findGoodsModelById('+models[i].mid+');"><i class="layui-icon">&#xe642;</i></button>'+
                            '<button type="button" class="layui-btn layui-btn-sm" onclick="deleteGoodsModel('+models[i].mid+');"><i class="layui-icon">&#xe640;</i></button></div></td>'+
                            '</tr>'
                    }
                    $("#showTotal").text('共有数据：'+data.modelInfo.total+'条');
                    $("#tab").html(str);
                }else {
                    totalPages=1;
                    totalCounts=1;
                    currentPage=1;
                    $("#tab").html('<tr><td colspan="8" align="center">没有用户数据请添加数据</td></tr>')
                }
            },error:function () {
                layer.alert("服务器错误",{icon:7})
            }
        })
    }
    function findModel() {
        findGoodsModelAll(1);
        pages();
    }
    function deleteGoodsModel(mId) {
        layer.confirm('是否删除这条记录?', {icon: 3, title:'删除'}, function(index){
            $.ajax({
                url:"../gm/deleteGoodsModelById",
                type:"get",
                data:{
                    "mId":mId,
                },
                dataType:"json",
                success:function (data) {
                    if (data.code == 200) {
                        layer.alert("删除成功",{icon:6},function () {
                            location.reload();
                        });
                    }else {
                        layer.alert("删除失败",{icon:5});
                    }
                },
                error:function () {
                    layer.alert("服务器错误",{icon:7});
                }
            })
            layer.close(index);
        });
    }
    function findGoodsModelById(mId) {
        $.ajax({
            url:"../gm/findGoodsModelById",
            type:"GET",
            async:false,
            data:{
                "mId":mId,
            },
            dataType:"json",
            success:function (data) {
                if (data.code == 200){
                    localStorage.setItem("mId",data.goodsModel.mid);
                    localStorage.setItem("mType",data.goodsModel.mtype);
                    localStorage.setItem("mName",data.goodsModel.mname);
                    localStorage.setItem("mRemarks",data.goodsModel.mremarks);
                    x_admin_show('更新型号信息','/jsp/model-edit.jsp',500,450);
                }
            }
        })
    }
</script>
</body>
</html>